//
//  AllCourseClassVC.m
//  HTP
//
//  Created by admin on 15/11/11.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "AllCourseClassVC.h"
#import "ClassCourseDetailViewController.h"
#import "DetailCourseRecordVC.h"
#import "CourseDetailCell.h"

#import "ClassCourseViewModel.h"
#import "NSDate+FSExtension.h"

@interface AllCourseClassVC ()<UITableViewDataSource,UITableViewDelegate,BaseTableDelgate>
@property (nonatomic,retain) BaseTableView *courseTable;

@property (nonatomic,retain) NSMutableArray *courseArray;
@end

@implementation AllCourseClassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(220, 220, 220, 1);
    _courseArray = [NSMutableArray array];
    [self.view addSubview:self.courseTable];
    [self getLocalData];
    [self getData];
    
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nsnofiationAction:) name:CHANGECOURSERECORD object:nil];
    // Do any additional setup after loading the view.
}
- (void)viewDidUnload{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self initNav];
}
//初始化导航栏
-(void)initNav{
    [self addCustomBackBase];
    [self addCustomTitleWithTitle:@"课程一览"];
}
//TODO:重写get方法
-(BaseTableView *)courseTable{
    if (!_courseTable) {
        _courseTable = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe) style:UITableViewStylePlain withHeadRefresh:YES andFoodRefresh:NO];
        _courseTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _courseTable.delegate = self;
        _courseTable.dataSource = self;
        _courseTable.tableHeaderView = nil;
        _courseTable.baseTableDelegate = self;
    }
    return _courseTable;
}


//TODO:tabledelegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _courseArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 125;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = UIView.new;
    [view setFrame:CGRectMake(0, 0, ScreenWidth, 0.1)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifity = @"coursedetailCell";
    CourseDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifity];
    if (!cell) {
        cell = [[CourseDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifity];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellData:[_courseArray objectAtIndex:indexPath.section]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailCourseRecordVC *detailCourserecordVC = [[DetailCourseRecordVC alloc] init];
    detailCourserecordVC.myClassRecord = [_courseArray objectAtIndex:indexPath.section];
    [self.navigationController pushViewController:detailCourserecordVC animated:YES];
}
//TODO:baseTableDelegate
-(void)headRefresh:tableView{
    [self getData];
}
//TODO:获取数据
-(void)getLocalData{
    NSDate *startDate = [[NSDate date] fs_dateBySubtractingDays:10];
    NSDate *endDate = [[NSDate date] fs_dateByAddingDays:30];
    NSPredicate *courseRecordInDates = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"cgcDate >= '%@' AND cgcDate <= '%@' AND studentId = %@",[startDate fs_stringWithFormat:kDateFormatdate],[endDate fs_stringWithFormat:kDateFormatdate],[NSNumber numberWithInt:[[USERCONTENT getUserParentId] intValue]]]];
    NSArray *array  = [CourseRecord findByPredicate:courseRecordInDates BySorted:@"cgcStatus" ascending:YES];
    _courseArray = (NSMutableArray *) [MyClassRecord toMyClassRecords:array];
    [self.courseTable reloadData];
   
}
- (void)getData{
    NSDate *startDate = [[NSDate date] fs_dateBySubtractingDays:10];
    NSDate *endDate = [[NSDate date] fs_dateByAddingDays:30];
    
    ClassCourseViewModel *classCourseViewModel = [[ClassCourseViewModel alloc] init];
    [classCourseViewModel setBlockWithReturnBlock:^(id returnValue) {
        [self getLocalData];
    } WithErrorBlock:nil WithFailureBlock:nil];
    [classCourseViewModel getCourseRecordByDates:startDate endDate:endDate];
}

- (void)nsnofiationAction:(NSNotification *)notification{
    [self getLocalData];
    [self getData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
